package main

import (
	"fmt"
	calculator "gitee.com/zhujiejun/go-mod-01/pi"
	"os"
	"runtime"
	"strconv"
	"time"
)

//var n int64 = 10000000
//var n int64 = 10000000000

func main() {
	//HE.Show(10)

	n, err := strconv.ParseInt(os.Args[1], 10, 64)
	if err != nil {
		fmt.Println("please input correct param")
		os.Exit(1)
	}

	var pi float64
	begin := time.Now()
	cn := runtime.NumCPU()
	runtime.GOMAXPROCS(cn)
	c := make(chan float64, cn)

	for i := 0; i < cn; i++ {
		go calculator.Calculate(n, int64(i)*n/int64(cn), (int64(i)+1)*n/int64(cn), c)
	}
	for i := 0; i < cn; i++ {
		pi += <-c
	}

	fmt.Println("the final Pi is: ", pi)
	end := time.Now()
	fmt.Printf("spend time: %vs\n", end.Sub(begin).Seconds())
}
